feat: add useAttribute hook for self-contained property-attribute reflection#97
Draft
cristinecula wants to merge 1 commit into
Draft
Conversation
|
✅ Deploy Preview for pionjs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
…lection Add a standalone useAttribute hook that provides bidirectional property <-> attribute sync with built-in loop prevention, type coercion, and MutationObserver-based attribute observation. Unlike useProperty with reflect, useAttribute is fully self-contained: no need to declare observedAttributes separately. It supports Boolean, String, and Number types with automatic coercion. API: const [value, setValue] = useAttribute(name, type, defaultValue?) Closes NEO-1055
7004a53 to
7dd8558
Compare
This was referenced Feb 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
useAttributehook that provides bidirectional property ↔ attribute sync with built-in type coercion, loop prevention, and MutationObserver-based attribute observationusePropertywithreflect,useAttributeis fully self-contained — no need to declareobservedAttributesseparatelyBoolean,String, andNumbertypes with automatic coercion and optional custom defaultsAPI
Signature:
useAttribute<T>(name: string, type: AttributeType, defaultValue?: T): StateTuple<T>How it works
observedAttributesdeclaration neededreflectingSymbol: when the hook reflects a property to an attribute, it sets a flag so the MutationObserver callback skips self-caused changesBooleantrue, absent →falsetrue→"",false→ removefalseStringsetAttribute(value)""NumberparseFloat(value)String(value)0Features
*-changedevent dispatch (cancelable, same convention asuseProperty)preventDefault()blocks both property and attribute updatesetCount(prev => prev + 1)lift()pattern for parent-controlled state<el opened>).prop=bindingsuseAttributecalls on same hostTest coverage
22 test cases covering:
All 91 tests pass (existing + new).
Files changed
src/use-attribute.tssrc/symbols.tsreflectingSymbolandattributeObserverSymbolsrc/core.tsuseAttributeexporttest/use-attribute.test.tsCloses NEO-1055